package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.ContactUsDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.contactus;

/**
 * Servlet implementation class ContactUsServlet
 */
public class ContactUsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContactUsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletpath = request.getServletPath();
		if ("/contactus/getAllContactUs".equals(servletpath)) {
			getAllContactUs(request, response);
		} else if ("/admin/contactus/insertContactUs".equals(servletpath)) {
			insertContactUs(request, response);
		} else if ("/contactus/viewContactUs".equals(servletpath)) {
			viewContactUs(request, response);
		} else if ("/admin/listContactUs".equals(servletpath)) {
			listContactUs(request, response);
		} else if ("/admin/contactus/viewContactUs".equals(servletpath)) {
			adminViewContactUs(request, response);
		} else if ("/admin/contactus/deleteContactUs".equals(servletpath)) {
			deleteContactUs(request, response);
		} else if ("/contactus/getCategoryContactUs".equals(servletpath)) {
			getCategoryContactUs(request, response);
		} else if ("/admin/contactus/updateContactUs".equals(servletpath)) {
			updateContactUs(request, response);
		} else if ("/admin/contactus/getCategoryContactUs".equals(servletpath)) {
			adminGetCategoryContactUs(request, response);
		}
	}
	
	protected void adminGetCategoryContactUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			ContactUsDao contactusDaoIns = new ContactUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/admin/contactus/getCategoryContactUs?category=" + category;
			List<?> NewsList = contactusDaoIns.getCategoryContactUs(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", contactusDaoIns.getDaoPage());
			contactusDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/admin/contactus/listContactUs.jsp");
		rd.forward(request, response);
	}

	protected void getAllContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			ContactUsDao contactusDaoIns = new ContactUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj//contactus/viewContactUs";
			List NewsList = contactusDaoIns.getAllContactUs(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", contactusDaoIns.getDaoPage());
			contactusDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/contactus/contactus.jsp");
		rd.forward(request, response);
	}
	
	protected void getCategoryContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			ContactUsDao contactusDaoIns = new ContactUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj//contactus/getCategoryContactUs?category=" + category;
			List NewsList = contactusDaoIns.getCategoryContactUs(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", contactusDaoIns.getDaoPage());
			contactusDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/contactus/contactus.jsp");
		rd.forward(request, response);
	}
	
	protected void updateContactUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ContactUsDao contactusDaoIns = new ContactUsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		contactus contactusIns = new contactus();
		contactusIns.setArticle(article);
		contactusIns.setAuthor(author);
		contactusIns.setDate(dateformat.format(date));
		contactusIns.setOriginal(original);
		contactusIns.setTitle(title);
		contactusIns.setCategory(category);
		contactusIns.setId(StringHandler.strToint(request.getParameter("id")));
		
		contactusDaoIns.updateContactUs(contactusIns);
		contactusDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listContactUs");
		rd.forward(request, response);
	}

	protected void insertContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ContactUsDao contactusDaoIns = new ContactUsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		contactus contactusIns = new contactus();
		contactusIns.setArticle(article);
		contactusIns.setAuthor(author);
		contactusIns.setDate(dateformat.format(date));
		contactusIns.setOriginal(original);
		contactusIns.setTitle(title);
		contactusIns.setCategory(category);

		contactusDaoIns.insertContactUs(contactusIns);
		contactusDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listContactUs");
		rd.forward(request, response);
	}

	protected void viewContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			ContactUsDao contactusDaoIns = new ContactUsDao();
			contactus contactusIns = contactusDaoIns.getSingleContactUs(newId);
			contactusDaoIns.closed();

			if (contactusIns != null) {
				request.setAttribute("newsIns", contactusIns);
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/contactus/viewContactUs.jsp");
		rd.forward(request, response);
	}

	protected void listContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			ContactUsDao contactusDaoIns = new ContactUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/admin/listContactUs";
			List NewsList = contactusDaoIns.getAllContactUs(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", contactusDaoIns.getDaoPage());
			contactusDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/contactus/listContactUs.jsp");
		rd.forward(request, response);
	}

	protected void adminViewContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			ContactUsDao contactusDaoIns = new ContactUsDao();
			contactus contactusIns = contactusDaoIns.getSingleContactUs(newId);
			contactusDaoIns.closed();

			if (contactusIns != null) {
				request.setAttribute("newsIns", contactusIns);
			}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/contactus/updateContactUs.jsp");
		rd.forward(request, response);
	}

	protected void deleteContactUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newId = request.getParameter("newId");
		ContactUsDao contactusDaoIns = new ContactUsDao();
		contactusDaoIns.deleteContactUs(newId);
		contactusDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listContactUs");
		rd.forward(request, response);

	}

}
